package com.it.system.searchservice.controller;

import com.it.system.educommonapi.entity.Course;
import com.it.system.searchservice.entity.PageEntity;
import com.it.system.searchservice.entity.ResponseResult;
import com.it.system.searchservice.feign.CourseFeignClient;
import com.it.system.searchservice.service.CourseIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class CourseIndexController {

    @Autowired
    private CourseIndexService courseIndexService;
    
    @Autowired
    private CourseFeignClient courseFeignClient;

    @GetMapping("/init")
    public ResponseEntity<String> initIndex() {
        try {
            courseIndexService.initCourseIndex();
            return ResponseEntity.ok("初始化成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("初始化失败: " + e.getMessage());
        }
    }
    
    /**
     * 搜索课程 (POST方法)
     * @param params 包含搜索参数：current, size, search, sort
     * @return 分页结果
     */
    @PostMapping("/course")
    public ResponseEntity<PageEntity> searchCoursePost(@RequestBody Map<String, String> params) {
        try {
            PageEntity result = courseIndexService.searchCourse(params);
            return ResponseEntity.ok(result);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }

    /**
     * 搜索课程 (GET方法)
     * @param current 当前页码
     * @param size 每页大小
     * @param search 搜索条件
     * @param sort 排序条件
     * @return 分页结果
     */
    @GetMapping("/courses")
    public ResponseEntity<PageEntity> searchCourseGet(
            @RequestParam(defaultValue = "0") String current,
            @RequestParam(defaultValue = "10") String size,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) String sort) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("current", current);
            params.put("size", size);
            if (search != null) {
                params.put("search", search);
            }
            if (sort != null) {
                params.put("sort", sort);
            }
            PageEntity result = courseIndexService.searchCourse(params);
            return ResponseEntity.ok(result);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }
    
    /**
     * 获取所有课程数据
     * @return 课程列表
     */
    @GetMapping("/courses/all")
    public ResponseEntity<List<Course>> getAllCourses() {
        ResponseResult<List<Course>> result = courseFeignClient.getAllCourse();
        if (result.getCode() == 200 && result.getData() != null) {
            return ResponseEntity.ok(result.getData());
        } else {
            return ResponseEntity.status(500).body(null);
        }
    }
}